package com.smxy.mall.dao.impl;

import com.smxy.mall.dao.CarDaoExt;
import com.smxy.mall.model.response.CarNumRes;
import com.smxy.mall.model.response.CarResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CarDaoImpl implements CarDaoExt {
    @Autowired
    NamedParameterJdbcTemplate jdbcTemplate;

    @Override
    public List<CarResponse> carList(Integer userId,Integer shopId) {
        Map<String, Object> sqlParamters = new HashMap<>();
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("SELECT c.id,c.goodsSize,c.num,c.goodsId,g.goodsName,g.cover,g.price,g.linePrice FROM fat_car as c,fat_goods as g WHERE c.userId = :userId AND c.shopId=:shopId AND c.goodsId=g.id");
        sqlParamters.put("userId", userId);
        sqlParamters.put("shopId", shopId);
        return jdbcTemplate.query(stringBuilder.toString(),sqlParamters, BeanPropertyRowMapper.newInstance(CarResponse.class));
    }

    @Override
    public List<CarNumRes> carNumList(Integer userId) {
        Map<String,Object> param = new HashMap<>();
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("SELECT goodsId,num FROM fat_car WHERE userId = :userId");
        param.put("userId",userId);
        return jdbcTemplate.query(stringBuilder.toString(),param,BeanPropertyRowMapper.newInstance(CarNumRes.class));
    }
}
